package com.lyf.scm.admin.domain.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.lyf.scm.admin.api.dto.PlanResponseDTO;
import com.lyf.scm.admin.api.dto.SalesPlanOtherAccuracyDTO;
import com.lyf.scm.admin.api.dto.SalesPlanOtherAccuracyInfoDTO;
import com.lyf.scm.admin.domain.remote.allocationservice.facade.SalesPlanOtherAccuracyFacade;
import com.lyf.scm.admin.domain.remote.basedata.facade.BaseDataFacade;
import com.lyf.scm.admin.domain.remote.item.facade.ItemCoreFacade;
import com.lyf.scm.admin.domain.service.SalesPlanOtherAccuracyService;
import com.lyf.scm.common.constants.CommonConstants;
import com.lyf.scm.common.enums.ResponseMsg;
import com.lyf.scm.common.model.ExcelExportDTO;
import com.lyf.scm.common.model.SalesPlanSelectDTO;
import com.lyf.scm.common.util.date.MyDateUtil;
import com.rome.arch.core.exception.RomeException;
import com.rome.user.common.utils.UserContext;
import feign.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * description:
 * 其他渠道计划_目标与销售计划准确率serviceImpl
 *
 * @author: haodong
 * @data: 2020/7/31
 */
@Slf4j
@Service
public class SalesPlanOtherAccuracyServiceImpl implements SalesPlanOtherAccuracyService {

    @Resource
    private SalesPlanOtherAccuracyFacade salesPlanOtherAccuracyFacade;

    @Resource
    private BaseDataFacade baseDataFacade;

    @Resource
    private ItemCoreFacade itemCoreFacade;

    /**
     * 按条件查询其他渠道计划_目标与销售计划准确率(周)
     * @param salesPlanOtherAccuracy
     * @return
     */
    @Override
    public PlanResponseDTO<SalesPlanOtherAccuracyInfoDTO> listWeekBySelective(SalesPlanOtherAccuracyDTO salesPlanOtherAccuracy) {
        //获取当前角色id(查询角色权限用)
        salesPlanOtherAccuracy.setCreator(UserContext.getUserId());
        //根据日期获取间隔日期集合
        List<Map<String, Date>> weekDates = MyDateUtil.getWeekDates(salesPlanOtherAccuracy.getStartTime(), 14);
        salesPlanOtherAccuracy.setWeekDateList(weekDates);

        //处理查询条件
        // List<SalesPlanSelectDTO> channelCodeList = baseDataFacade.getSubChannelCode(salesPlanOtherAccuracy.getChannelCodeList());

        List<SalesPlanSelectDTO> areaCodeList = baseDataFacade.getSubAreaCode(salesPlanOtherAccuracy.getAreaCodeList());

        List<SalesPlanSelectDTO> categoryCodeList = itemCoreFacade.getSubcategoryCode(salesPlanOtherAccuracy.getCategoryCodeList());

        // BaseSearchUtil.setSearchFields(salesPlanOtherAccuracy, channelCodeList, categoryCodeList, areaCodeList);

        PageInfo<SalesPlanOtherAccuracyInfoDTO> pageInfo = salesPlanOtherAccuracyFacade.listWeekBySelective(salesPlanOtherAccuracy);
        PlanResponseDTO<SalesPlanOtherAccuracyInfoDTO> planResponse =  new PlanResponseDTO<>();
        planResponse.setInfoList(pageInfo.getList());
        //格式化日期格式
        planResponse.setDateList(MyDateUtil.processCyclesDate(weekDates, "M.dd"));
        planResponse.setTotal(pageInfo.getTotal());
        return planResponse;
    }

    /**
     * 图表数据接口
     *
     * @param salesPlanOtherAccuracy
     * @return
     */
    @Override
    public PlanResponseDTO<SalesPlanOtherAccuracyInfoDTO> eChartsData(SalesPlanOtherAccuracyDTO salesPlanOtherAccuracy) {
        //获取当前角色id(查询角色权限用)
        salesPlanOtherAccuracy.setCreator(UserContext.getUserId());
        Date startTime = salesPlanOtherAccuracy.getStartTime();
        Date endTime = salesPlanOtherAccuracy.getEndTime();
        Integer type = null;
        List<SalesPlanOtherAccuracyInfoDTO> SalesPlanOtherAccuracyInfoDTOList = null;
        //周、月维度查询
        List<Map<String, Date>> weekDates = null;
        if (null != endTime) {
            //根据开始时间和结束时间，判断间隔周期长度，小于一个月，按天返回，大于一个月，按周返回，大于一个季度，按月返回
            type = MyDateUtil.getTypeBetweenDays(startTime, endTime);
            if (type == 1) {
                weekDates = MyDateUtil.getMonthDates(startTime, endTime);
                salesPlanOtherAccuracy.setWeekDateList(weekDates);
            } else {
                weekDates = MyDateUtil.getWeekDates(startTime, endTime);
                salesPlanOtherAccuracy.setWeekDateList(weekDates);
            }
        } else {
            //默认按周显示
            weekDates = MyDateUtil.getWeekDates(startTime, CommonConstants.WEEK_SIZE);
            salesPlanOtherAccuracy.setWeekDateList(weekDates);
        }
        SalesPlanOtherAccuracyInfoDTOList = salesPlanOtherAccuracyFacade.listAllByCycles(salesPlanOtherAccuracy);
        PlanResponseDTO<SalesPlanOtherAccuracyInfoDTO> planResponse = new PlanResponseDTO<>();
        planResponse.setInfoList(SalesPlanOtherAccuracyInfoDTOList);
        planResponse.setDateList(MyDateUtil.processCyclesDate(weekDates, "M.dd"));
        return planResponse;

    }

    @Override
    public Response export(ExcelExportDTO excelExportDTO, Integer type) {
        if (ObjectUtil.isEmpty(type)) {
            throw new RomeException(ResponseMsg.FAIL_2005.getCode(), ResponseMsg.FAIL_2005.getMsg());
        }
        SalesPlanOtherAccuracyDTO salesPlanOtherAccuracyDTO=new SalesPlanOtherAccuracyDTO();
        salesPlanOtherAccuracyDTO.setLayeredCodeList(excelExportDTO.getLayeredCodeList());
        salesPlanOtherAccuracyDTO.setChannelCodeList(excelExportDTO.getChannelCodeList());
        salesPlanOtherAccuracyDTO.setAreaCodeList(excelExportDTO.getAreaCodeList());
        salesPlanOtherAccuracyDTO.setCategoryCodeList(excelExportDTO.getCategoryCodeList());
        salesPlanOtherAccuracyDTO.setPageNum(excelExportDTO.getPageNum());
        salesPlanOtherAccuracyDTO.setPageSize(excelExportDTO.getPageSize());
        if(ObjectUtil.isNotEmpty(excelExportDTO.getBaseSearchS())){
            List<SalesPlanOtherAccuracyDTO> salesPlanOtherAccuracyList=new ArrayList<>();
            excelExportDTO.getBaseSearchS().forEach(item->{
                SalesPlanOtherAccuracyDTO dto=new SalesPlanOtherAccuracyDTO();
                dto.setChannelCode(item.getChannelCode());
                dto.setSubChannelCode(item.getSubChannelCode());
                dto.setProvinceCode(item.getProvinceCode());
                dto.setCityCode(item.getCityCode());
                dto.setCountyCode(item.getCountyCode());
                dto.setStoreCode(item.getStoreCode());
                dto.setPriCategoryCode(item.getPriCategoryCode());
                dto.setSecCategoryCode(item.getSecCategoryCode());
                dto.setTerCategoryCode(item.getTerCategoryCode());
                dto.setSkuCode(item.getSkuCode());
                dto.setType(item.getType());
                dto.setCreator(UserContext.getUserId());
                salesPlanOtherAccuracyList.add(dto);
            });
            salesPlanOtherAccuracyDTO.setSalesPlanOtherAccuracyList(salesPlanOtherAccuracyList);
        }
        //获取当前角色id(查询角色权限用)
        salesPlanOtherAccuracyDTO.setCreator(UserContext.getUserId());
        //处理开始时间
        salesPlanOtherAccuracyDTO.setStartTime(excelExportDTO.getStartTime());
        List<Map<String, Date>> weekDate = null;
        //选择时间范围导出
        if (ObjectUtil.isNotNull(excelExportDTO.getEndTime())) {
            weekDate = MyDateUtil.getWeekDates(salesPlanOtherAccuracyDTO.getStartTime(),excelExportDTO.getEndTime());
        } else {
            //根据日期获取间隔日期集合,没有结束时间，默认14周
            weekDate = MyDateUtil.getWeekDates(salesPlanOtherAccuracyDTO.getStartTime(), CommonConstants.WEEK_SIZE);
        }
        salesPlanOtherAccuracyDTO.setWeekDateList(weekDate);
        //计算出默认的结束时间
        salesPlanOtherAccuracyDTO.setEndTime(MyDateUtil.getLastDate(null, weekDate));
        salesPlanOtherAccuracyDTO.setUnitType(excelExportDTO.getUnitType());
        //获取三个查询条件的实际集合
        // List<SalesPlanSelectDTO> channelCodeList = baseDataFacade.getSubChannelCode(excelExportDTO.getChannelCodeList());
        // List<SalesPlanSelectDTO> categoryCodeList = itemCoreFacade.getSubcategoryCode(excelExportDTO.getCategoryCodeList());
        // List<SalesPlanSelectDTO> areaCodeList = baseDataFacade.getSubAreaCode(excelExportDTO.getAreaCodeList());
        // BaseSearchUtil.setSearchFields(salesPlanOtherAccuracyDTO, channelCodeList, categoryCodeList, areaCodeList);
        return salesPlanOtherAccuracyFacade.export(salesPlanOtherAccuracyDTO,type);
    }



}